# encoding=utf-8
from LBT.Resource.StudentInfo.StudentInfoAtt import StudentInfoAtt
from LBT.Resource.BaseAtt import BaseAtt
from LBT.GeneralOperation.BaseOperation import BaseOperation
from Parents.Tools.mytools import tools


class StudentOperation(StudentInfoAtt, BaseOperation, BaseAtt):
    '''
    幼儿档案通用操作
    '''

    def __init__(self, dr):
        self.dr = dr

    def getstudentinfo(self, student):
        """
        通过传入学生一整行信息，判断学生详细信息，返回序号、姓名、性别、班级、手机号
        :param studentelement
        """
        dr = self.dr
        studentinfo = student.text
        # 下方td，1、序号2、姓名3、性别4、班级（年级）5、第一监护人手机号
        infos = str(studentinfo).split(" ")
        infodict = {}
        infodict['name'] = str(infos[1])
        infodict['class'] = str(infos[3])
        infodict['sex'] = str(infos[2])
        infodict['phone'] = str(infos[4])
        return infodict

    def writeImportStudentExcel(self, file, infolist):
        """
        将信息写入导入学生模板
        :param file:将要写入的文件
        :param infolist:学生信息
        :return:
        """
        try:
            list = []
            for i in range(infolist.__len__()):
                dict = {}
                dict['row'] = i + 1
                dict['col'] = 0
                dict["info"] = infolist[i]["name"]
                list.append(dict)
                dict = {}
                dict['row'] = i + 1
                dict['col'] = 1
                dict["info"] = infolist[i]["sex"]
                list.append(dict)
                dict = {}
                dict['row'] = i + 1
                dict['col'] = 2
                dict["info"] = str(infolist[i]["class"]).split("(")[0]
                list.append(dict)
                dict = {}
                dict['row'] = i + 1
                dict['col'] = 3
                dict["info"] = infolist[i]["phone"]
                list.append(dict)
                i += 1
            tools().writeExcle(file, list)
            return True
        except Exception as e:
            raise e
    def readErrorStudentExcel(self,file):
        """
        跟进传入的文件名，返回excel里面所有内容，存放list
        :param file: 传入文件名
        :return:
        """
        return  tools().readExcle(file)
